package Lyl.wiki.controller;

import Lyl.wiki.req.CategoryQueryReq;
import Lyl.wiki.req.CategorySaveReq;
import Lyl.wiki.resp.CommonResp;
import Lyl.wiki.resp.CategoryQueryResp;
import Lyl.wiki.resp.PageResp;
import Lyl.wiki.service.CategoryService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Resource
    private CategoryService categoryService;
    @GetMapping("/list")
    public CommonResp list(@Valid CategoryQueryReq req){
        //把查询结果放到列表中
        CommonResp<PageResp<CategoryQueryResp>> resp =new CommonResp<>();
        //创建一个CommonResp类型对象resp
        PageResp<CategoryQueryResp> list =categoryService.list(req);
        //将resp对象中的内容设置为查询结果列表list
        resp.setContent(list);
        //返回resp对象
        return resp;
    }

    @GetMapping("/all")
    public CommonResp all(){
        //把查询结果放到列表中
        CommonResp<List<CategoryQueryResp>> resp =new CommonResp<>();
        //创建一个CommonResp类型对象resp
        List<CategoryQueryResp> list =categoryService.all();
        //将resp对象中的内容设置为查询结果列表list
        resp.setContent(list);
        //返回resp对象
        return resp;
    }
    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody CategorySaveReq req){
        CommonResp resp =new CommonResp<>();
        categoryService.save(req);
        return resp;
    }

    @DeleteMapping("/delete/{id}")
    public CommonResp delete(@PathVariable Long id){
        CommonResp resp = new CommonResp<>();
        categoryService.delete(id);
        return resp;
    }
}

